package com.gitee.wsl.ext.array

import kotlin.collections.indexOf

fun<T> Array<T>.remove(element: T): Array<T> {
    val index = indexOf(element)
    if(index >= 0){
        return remove(index)
    }
    return this
}

fun<T> Array<T>.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}


/**
 * Removes the element at the specified position in this IntArray.
 *
 * @param pos the position of the element to be removed
 * @return the new IntArray with the element removed
 */
fun IntArray.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}

/**
 * Removes elements from the array with indices specified in the given list.
 *
 * @param indices the list of element indices to be removed from the array.
 * @return the new array with requested elements removed, or the original array if the list is empty.
 */
fun IntArray.removeAll(indices: List<Int>) = when {
    indices.isEmpty() -> this
    indices.size == 1 -> remove(indices.first())
    else -> this.filterIndexed { index, _ -> index !in indices }.toIntArray()
}

fun FloatArray.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}

fun DoubleArray.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}

fun LongArray.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}

fun CharArray.remove(pos: Int) = when (pos) {
    0 -> sliceArray(1..lastIndex)
    lastIndex -> sliceArray(0 until lastIndex)
    else -> sliceArray(0 until pos) + sliceArray(pos + 1..lastIndex)
}

/**
 * Removes the first [take] number of [T] objects.
 * @throws NoSuchElementException if this deque does not have enough elements
 */
fun <T> ArrayDeque<T>.removeFirst(take: Int): Collection<T>
{
    val list = mutableListOf<T>()
    (1..take).forEach { _ -> list.add(removeFirst()) }
    return list
}